package com.yhsoft.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhsoft.bill.service.ISalerYszkService;
import com.yhsoft.common.response.PageResult;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.common.vo.Result;
import com.yhsoft.sys.controller.Response.CustomerOutVo;
import com.yhsoft.sys.controller.request.CustomerAddInVo;
import com.yhsoft.sys.controller.request.CustomerSelectInVo;
import com.yhsoft.sys.controller.request.CustomerSelectorOutVo;
import com.yhsoft.sys.controller.request.CustomerUpdateInVo;
import com.yhsoft.sys.entity.CustomerInfo;
import com.yhsoft.sys.service.ICustomerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客户企业管理 前端控制器
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-10-15
 */
@Api(tags = {"F-客户管理相关接口"})
@RestController
@RequestMapping("/sys/customerInfo")
public class CustomerInfoController {

    @Resource
    private ICustomerInfoService iCustomerInfoService;

    @Resource
    private ISalerYszkService iSalerYszkService;

    @ApiOperation("新增客户企业基本信息")
    @PostMapping("/addCustomerInfo")
    public Result<?> addCustomerInfo(@RequestBody CustomerAddInVo inVo){
        // 新增企业基本信息表
        String customerId = iCustomerInfoService.addCustomerInfo(inVo);
        // 新增渠道商应收账款信息表
        if(StringUtils.hasLength(customerId)){
            iSalerYszkService.addYszk(customerId);
        }
        return Result.success("新增客户企业基本信息成功！");
    }

    @ApiOperation("修改客户企业基本信息")
    @PostMapping("/updateCustomerInfo")
    public Result<?> updateCustomerInfo(@Valid @RequestBody CustomerUpdateInVo inVo){
        iCustomerInfoService.updateCustomerInfo(inVo);
        return Result.success("修改客户企业基本信息成功！");
    }


    @ApiOperation("删除客户企业信息")
    @PostMapping("/delCustomer")
    public Result<?> delCustomer(@RequestParam(value = "id", required = true) List<Integer> ids){
        iCustomerInfoService.delCustomer(ids);
        return Result.success("删除删除客户企业信息成功！");
    }

    @ApiOperation("获得客户企业信息列表")
    @GetMapping("/getCustomerList")
    public Result<PageResult<CustomerOutVo>> getCustomerList(@Valid CustomerSelectInVo inVo){
        IPage<CustomerOutVo> iPage = iCustomerInfoService.getCustomerList(inVo);
        return Result.success(PageResult.valueOf(iPage));
    }

    @ApiOperation("客户企业选择器")
    @GetMapping("/selector")
    public Result<List<CustomerSelectorOutVo>> selector(@RequestParam(value = "customerName", required = false) String customerName){
        LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerInfo::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        queryWrapper.like(StringUtils.hasLength(customerName),CustomerInfo::getCustomerName,customerName);
        List<CustomerInfo> list = iCustomerInfoService.list(queryWrapper);
        List<CustomerSelectorOutVo> outVoList = new ArrayList<>();
        if(list.size() > 0){
            list.forEach(a -> {
                outVoList.add(new CustomerSelectorOutVo(a.getId(),a.getCustomerName()));
            });
        }
        return Result.success(outVoList);
    }

}
